package xyz.xwls.bootadmin.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.xwls.bootadmin.common.BootAdminException;
import xyz.xwls.bootadmin.common.CommonResult;
import xyz.xwls.bootadmin.common.ResultCode;

/**
 * @author WZW
 * @date 2020/4/9
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionControllerAdvice {

    @ExceptionHandler
    public CommonResult<Object> exceptionHandler(Exception e){
        log.error("exception type : {}", e.getClass());
        log.error("exception : {}", e.getMessage());
        return CommonResult.failed(e.getClass().getName());
    }

    /**
     * 参数校验失败
     * @param e 异常对象
     * @return 错误信息
     */
    @ExceptionHandler
    public CommonResult<Object> exceptionHandler(HttpMessageNotReadableException e){
        log.error("exception : {}", e.getMessage());
        return CommonResult.failed(ResultCode.VALIDATE_FAILED);
    }

    @ExceptionHandler
    public CommonResult<Object> exceptionHandler(BootAdminException e){
        CommonResult<Object> result = CommonResult.failed(e.getMessage());
        result.setCode(e.getCode());
        return result;
    }

}
